package cn.itcast.controller;


import cn.itcast.service.IChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;


@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class CustomerServiceController {
    @Autowired
    private ChatClient aliModel;//注意bean的名字已经换成了aliModel
    @Autowired
    private IChatHistoryService chatHistoryService;
    
    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> service(@RequestParam("prompt") String prompt, @RequestParam("chatId") String chatId) {
        chatHistoryService.saveChatHistory("service",chatId);
        return aliModel.prompt()
                .user(prompt)
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatId))
                .stream()
                .content();
    }
}